package com.chunfeng.mypen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chunfeng.mypen.dao.TopicDao;
import com.chunfeng.mypen.dao.TopicExamDao;
import com.chunfeng.mypen.entity.Topic;
import com.chunfeng.mypen.entity.TopicExam;
import com.chunfeng.mypen.result.JsonRequest;
import com.chunfeng.mypen.result.exenum.RequestException;
import com.chunfeng.mypen.service.TopicService;
import com.chunfeng.mypen.vo.TopicVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;

import java.util.List;

/**
 * @Description: 做题历史记录实现类
 * @author: 小雷
 * @createTime: 2023-11-14 00:17:10
 */
@Service
public class TopicServiceImpl extends ServiceImpl<TopicDao, Topic> implements TopicService {

    @Autowired
    private TopicDao topicDao;
    @Autowired
    private TopicExamDao topicExamDao;

    /**
     * 条件查询所有题目
     *
     * @param topic 条件
     * @return JSON
     */
    @GetMapping
    @Override
    public JsonRequest<List<TopicVo>> lookTopic(Topic topic) {
        List<TopicVo> topicVoList = topicDao.lookTopic(topic);
        if (CollectionUtils.isEmpty(topicVoList) || topicVoList.size() < 1) {
            JsonRequest.error(RequestException.SELECT_ERROR);
        }
        return JsonRequest.success(topicVoList);
    }

    /**
     * 添加题目
     *
     * @param topic 实体类
     * @return JSON
     */
    @PostMapping
    @Override
    public JsonRequest<Integer> addTopic(Topic topic) {
        int insert = this.baseMapper.insert(topic);
        if (insert < 1) {
            JsonRequest.error(RequestException.INSERT_ERROR);
        }
        return JsonRequest.success(insert);
    }

    /**
     * 修改题目
     *
     * @param topic 实体类
     * @return JSON
     */
    @PutMapping
    @Override
    public JsonRequest<Integer> updateTopic(Topic topic) {
        int update = this.baseMapper.updateById(topic);
        if (update < 1) {
            JsonRequest.error(RequestException.UPDATE_ERROR);
        }
        return JsonRequest.success(update);
    }

    /**
     * 删除题目
     *
     * @param ids 需要删除的id
     * @return JSON
     */
    @DeleteMapping
    @Override
    public JsonRequest<Integer> deleteTopic(List<String> ids) {
        //判断接收的参数是否为空
        if (CollectionUtils.isEmpty(ids) || ids.size() < 1) {
            return JsonRequest.error(RequestException.DELETE_ERROR);
        }
        //判断id是否存在
        Integer count = topicDao.selectCount(new LambdaQueryWrapper<Topic>().in(Topic::getId, ids));
        if (count != ids.size()) {
            return JsonRequest.error(RequestException.DELETE_ERROR);
        }
        //删除题目和试卷关联的数据
        topicExamDao.delete(new LambdaQueryWrapper<TopicExam>().in(TopicExam::getTopicId, ids));
        //删除题目
        int delete = this.baseMapper.deleteBatchIds(ids);
        if (delete != ids.size()) {
            return JsonRequest.error(RequestException.DELETE_ERROR);
        }
        return JsonRequest.success(delete);
    }
}
